using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using QASPRibbon;
using QASPRibbon.Controls;

namespace QASPRibbonO365Demo.Features.Ribbon
{
    [Guid("bc8ee845-277c-47f6-9926-655f6a3b080c")]
    public class RibbonEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            var parent = properties.Feature.Parent as SPWeb;
            if(parent!=null)
                RegisterTab(parent);
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            var parent = properties.Feature.Parent as SPWeb;
            if (parent != null)
                RibbonCustomAction.RemoveAllCustomizations(parent, _cutomizationId);
        }

        private readonly Guid _cutomizationId = new Guid("875ef56b-a37c-441a-8de4-0afe6fb762f8");

        private void RegisterTab(SPWeb web)
        {
            var ca = new RibbonCustomAction();
            var tab = new RibbonTab("QASPRibbon", "QASPRibbon O365")
                {
                    Sequence = 1001,
                    Groups = new []
                        {
                            new RibbonGroup("First", "Demo group")
                                {
                                    Sections = new []
                                        {
                                            new RibbonGroupSection
                                                {
                                                    Type = RibbonSectionType.OneRow,
                                                    DisplayMode = RibbonDisplayMode.Image32AndCaption,
                                                    Controls = new RibbonControl[]
                                                        {
                                                            new RibbonButton("Button1", "Button 1")
                                                                {
                                                                    Image = RibbonImageDefinition.Standard15(12,7),
                                                                    OnClientClick = "alert('b1');"
                                                                }
                                                        }
                                                },
                                                RibbonGroupSection.Divider,
                                                new RibbonGroupSection
                                                    {
                                                        Type = RibbonSectionType.TwoRows,
                                                        Alignment = RibbonSectionAlignment.Top,
                                                        DisplayMode = RibbonDisplayMode.Image16AndCaption,
                                                        Controls = new RibbonControl[]
                                                            {
                                                                new RibbonButton("b2", "Button 2")
                                                                {
                                                                    Image = RibbonImageDefinition.Standard15(12,7),
                                                                    OnClientClick = "alert('b2');"
                                                                },
                                                                new RibbonButton("b3", "Button 3")
                                                                {
                                                                    Image = RibbonImageDefinition.Standard15(9,7),
                                                                    OnClientClick = "alert('b3');"
                                                                },
                                                                new RibbonButton("b4", "Button 4")
                                                                {
                                                                    Image = RibbonImageDefinition.Standard15(2,7),
                                                                    OnClientClick = "alert('b4');"
                                                                }
                                                            }
                                                    }
                                        }
                                } 
                        }
                };
            ca.AddTab(tab);
            ca.Provision(_cutomizationId, web, ListTypes.All, ListForms.All);
        }
    }
}
